package com.dmagic.cms.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dmagic.cms.common.dao.CommonDao;
import com.dmagic.cms.common.exception.ServiceException;
import com.dmagic.cms.common.service.CommonService;
import com.dmagic.cms.common.util.ListUtil;
import com.dmagic.cms.common.util.reflection.BeanUtil;
import com.dmagic.cms.dao.ProductCategoryDao;
import com.dmagic.cms.entity.ProductCategory;
import com.dmagic.cms.service.ProductCategoryService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Service
@Transactional(readOnly = true)
public class ProductCategoryServiceImpl extends CommonService<ProductCategory> implements
		ProductCategoryService {

	@Autowired
	private ProductCategoryDao productCategoryDao;

	@Override
	protected CommonDao<ProductCategory, Serializable> getCommonDao() {
		return (CommonDao) productCategoryDao;
	}

	@Override
	@Transactional(readOnly = false)
	public void saveCategory(List<Map> categorys) throws ServiceException {
		List<ProductCategory> entitys = Lists.newArrayList();
		for (Map category : categorys) {
			ProductCategory parent = new ProductCategory();
			BeanUtil.mapToBean(category, parent);
			entitys.add(parent);
			List<Map> subCategorys = (List<Map>) category.get("subCategorys");
			if (ListUtil.isNotEmpty(subCategorys)) {
				parent.setSubCategorys(null);
				for (Map m : subCategorys) {
					ProductCategory c = BeanUtil.mapToBean(m, ProductCategory.class);
					c.setParentCategoryId(parent.getProductCategoryId());
					parent.addSubCategory(c);
				}
				if (ListUtil.isNotEmpty(parent.getSubCategorys())) {
					entitys.addAll(parent.getSubCategorys());
				}
			}
		}
		productCategoryDao.batchSaveCategorys(entitys);
	}
	
	@Override
	@Transactional(readOnly = false)
	public void saveCategory2(List<Map> categorys) throws ServiceException {
		List<ProductCategory> entitys = Lists.newArrayList();
		Map<Long, Long> map = Maps.newHashMap();
		for (Map category : categorys) {
			ProductCategory c = new ProductCategory();
			BeanUtil.mapToBean(category, c);
			if(c.getParentCategoryId() == null)
				continue;
			if(c.getProductCategoryId()<0 && c.getParentCategoryId()>0){
				Long tempId= c.getProductCategoryId();
				c.setProductCategoryId(null);
				productCategoryDao.saveEntity(c);
				map.put(tempId, c.getProductCategoryId());
			}else if(c.getParentCategoryId()<0){
				entitys.add(c);
			}else{
				productCategoryDao.saveEntity(c);
			}
		}
		for (ProductCategory c : entitys) {
				c.setProductCategoryId(null);
				c.setParentCategoryId(map.get(c.getParentCategoryId()));
				productCategoryDao.saveEntity(c);
		}
		//productCategoryDao.batchSaveCategorys(entitys);
	}
	
	@Override
	public List<ProductCategory> getAllParentCategory() {
		return productCategoryDao.getAllParentCategory();
	}

	@Override
	public List<ProductCategory> getAllProductCategoryByLanguage(Long languageId) {
		return productCategoryDao.getAllProductCategoryByLanguage(languageId);
	}

	@Override
	@Transactional(readOnly = false)
	public void deleteCategory(Long categorysId) throws ServiceException {
		ProductCategory c = getEntityById(categorysId);
		if (ListUtil.isNotEmpty(c.getSubCategorys())) {
			for (ProductCategory sc : c.getSubCategorys()) {
				deleteEntity(sc);
			}
		}
		deleteEntityById(categorysId);
	}

	@Override
	public List<ProductCategory> getSuccessfulCaseCategoryByLanguage(Long languageId) {
		return productCategoryDao.getSuccessfulCaseCategoryByLanguage(languageId);
	}
}
